package com.mioto.ld.module.device.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mioto.ld.aop.anno.BizLogAnno;
import com.mioto.ld.module.log.model.BizModuleName;
import com.mioto.ld.module.log.model.BizOperationType;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.mioto.ld.module.device.model.DevicePressTimeLog;
import com.mioto.ld.module.device.service.IDevicePressTimeLogService;
import com.mioto.ld.module.statistics.StatisticsFactory;
import com.mioto.ld.module.statistics.model.MaterialSaleSearchDTO;
import com.mioto.ld.module.statistics.model.MaterialSaleStatisticsVO;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-06-13 20:16:51
 */
@RestController
@RequestMapping("devicePressTimeLog")
@Api(tags = "设备按键时长上报记录管理")
public class DevicePressTimeLogController {
    @Resource
    private IDevicePressTimeLogService devicePressTimeLogService;

    @GetMapping
    @ApiOperation(value="查询设备按键时长上报记录列表",response = DevicePressTimeLog.class)
    public ResultData list (HttpServletRequest request,DevicePressTimeLog devicePressTimeLog){
        return ResultData.success(devicePressTimeLogService.findList(devicePressTimeLog));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询设备按键时长上报记录",response = DevicePressTimeLog.class)
    public ResultData pager (HttpServletRequest request, DevicePressTimeLog devicePressTimeLog,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<DevicePressTimeLog> list = devicePressTimeLogService.findList(devicePressTimeLog);
        PageInfo<DevicePressTimeLog> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询设备按键时长上报记录",response = DevicePressTimeLog.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(devicePressTimeLogService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增设备按键时长上报记录")
    public ResultData add (DevicePressTimeLog devicePressTimeLog){
        return Optional.of(devicePressTimeLogService.insert(devicePressTimeLog))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(devicePressTimeLog))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新设备按键时长上报记录 局部更新,忽略空值")
    public ResultData updateIgnoreNull (DevicePressTimeLog devicePressTimeLog){
        return Optional.of(devicePressTimeLogService.updateIgnoreNull(devicePressTimeLog))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(devicePressTimeLog))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新设备按键时长上报记录 整体更新")
    public ResultData update (DevicePressTimeLog devicePressTimeLog){
        return Optional.of(devicePressTimeLogService.update(devicePressTimeLog))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(devicePressTimeLog))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除设备按键时长上报记录")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(devicePressTimeLogService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除设备按键时长上报记录")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(devicePressTimeLogService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @GetMapping("/saleStatistics")
    @ApiOperation(value="材料销量统计",response = MaterialSaleStatisticsVO.class)
    @BizLogAnno(operationType =  BizOperationType.MATERIAL_SALE_STATISTIC,moduleName = BizModuleName.STATISTICS)
    public ResultData saleStatistics(MaterialSaleSearchDTO search){
        return ResultData.success(StatisticsFactory.create(search.getSearchType()).materialSaleStatistics(search));
    }
}